using System.Text.Json.Serialization;

namespace Sage.Http.Options
{
    /// <summary>
    /// 熔断器配置选项
    /// </summary>
    public class CircuitBreakerOptions
    {
        /// <summary>
        /// 是否启用熔断器
        /// </summary>
        [JsonPropertyName("Enabled")]
        public bool Enabled { get; set; } = true;

        /// <summary>
        /// 触发熔断的失败次数阈值
        /// </summary>
        [JsonPropertyName("FailureThreshold")]
        public int FailureThreshold { get; set; } = 5;

        /// <summary>
        /// 熔断后的恢复超时时间（毫秒）
        /// </summary>
        [JsonPropertyName("RecoveryTimeMs")]
        public int RecoveryTimeMs { get; set; } = 30000;

        /// <summary>
        /// 熔断器半开状态时允许的试探请求数
        /// </summary>
        [JsonPropertyName("HalfOpenMaxRequests")]
        public int HalfOpenMaxRequests { get; set; } = 1;

        /// <summary>
        /// 触发熔断的HTTP状态码
        /// </summary>
        [JsonPropertyName("TripStatusCodes")]
        public int[] TripStatusCodes { get; set; } = [500, 502, 503, 504];
        
        /// <summary>
        /// 创建当前选项的副本
        /// </summary>
        public CircuitBreakerOptions Clone()
        {
            return new CircuitBreakerOptions
            {
                Enabled = this.Enabled,
                FailureThreshold = this.FailureThreshold,
                RecoveryTimeMs = this.RecoveryTimeMs,
                HalfOpenMaxRequests = this.HalfOpenMaxRequests,
                TripStatusCodes = (int[])this.TripStatusCodes.Clone()
            };
        }
    }
}